export const calcDate = (date1, date2) => {
  var date3 = date2 - date1

  var days = Math.floor(date3 / (24 * 3600 * 1000))

  var leave1 = date3 % (24 * 3600 * 1000) // 计算天数后剩余的毫秒数
  var hours = Math.floor(leave1 / (3600 * 1000))

  var leave2 = leave1 % (3600 * 1000) // 计算小时数后剩余的毫秒数
  var minutes = Math.floor(leave2 / (60 * 1000))

  var leave3 = leave2 % (60 * 1000) // 计算分钟数后剩余的毫秒数
  var seconds = Math.round(date3 / 1000)
  return {
    leave1,
    leave2,
    leave3,
    days: days,
    hours: hours,
    minutes: minutes,
    seconds: seconds
  }
}

/**
 * 日期格式化
 */
export function dateFormat(date, formats) {
  let format = formats || 'yyyy-MM-dd hh:mm:ss'
  if (date !== 'Invalid Date') {
    var o = {
      'M+': date.getMonth() + 1, // month
      'd+': date.getDate(), // day
      'h+': date.getHours(), // hour
      'm+': date.getMinutes(), // minute
      's+': date.getSeconds(), // second
      'q+': Math.floor((date.getMonth() + 3) / 3), // quarter
      'S': date.getMilliseconds() // millisecond
    }
    if (/(y+)/.test(format)) {
      format = format.replace(RegExp.$1,
        (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    for (var k in o) {
      if (new RegExp('(' + k + ')').test(format)) {
        format = format.replace(RegExp.$1,
          RegExp.$1.length === 1 ? o[k]
            : ('00' + o[k]).substr(('' + o[k]).length))
      }
    }
    return format
  }
  return ''
}

export const getCurrentDate = (type) => {
  var date = new Date()
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  var nowDate
  if(type && type === 'month') {
    nowDate = year + '-' + (month < 10 ? '0' + month : month)
  } else if (type && type === 'year') {
    nowDate = year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day)
  } else {
    nowDate = year + '年' + (month < 10 ? '0' + month : month) + '月' + (day < 10 ? '0' + day : day) + '日'
  }
  
  return nowDate
}

export const getMonthFirstDate = (type, time) => {
  var date = time ? new Date(time) : new Date()
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var nowDate
  if(type && type === 'month') {
    nowDate = year + '-' + (month < 10 ? '0' + month : month)
  } else if (type && type === 'year') {
    nowDate = year + '-' + (month < 10 ? '0' + month : month) + '-01'
  } else {
    nowDate = year + '年' + (month < 10 ? '0' + month : month) + '月' + '01日'
  }
  
  return nowDate
}
// 获取某个月最后一天
export const getMonthLastDate = (time) => {
  var date = new Date(time)
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var lastDay= new Date(year, month, 0).getDate()  //31
  var nowDate
  nowDate = year + '-' + (month < 10 ? '0' + month : month) +  '-' +  lastDay
  return nowDate
}
// 多少天以前
export const getCurrentDateNago = (num, type) => {
  var nowDate = new Date().getTime()
  var agoData = new Date(nowDate - num * 24 * 60 * 60 * 1000)
  var year = agoData.getFullYear()
  var month = agoData.getMonth() + 1
  var day = agoData.getDate()
  const hours = agoData.getHours()
  const minutes = agoData.getMinutes()
  const seconds = agoData.getSeconds()
  const m = month < 10 ? '0' + month : month
  const d = day < 10 ? '0' + day : day
  const h = hours < 10 ? '0' + hours : hours
  const min = minutes < 10 ? '0' + minutes : minutes
  const s = seconds < 10 ? '0' + seconds : seconds
  return type === 'time'
    ? `${year}-${m}-${d} ${h}:${min}:${s}`
    : `${year}-${m}-${d}`
}
// 几个月后的今天
export const getAgoMonthDate = (num) => {
  var nowMonth, newYear
  var date = new Date()
  var year = date.getFullYear()
  var month = date.getMonth() + 1 + num
  if (month > 12) {
    nowMonth = month - 12
    newYear = year + 1
  } else {
    nowMonth = month
    newYear = year
  }
  var day = date.getDate()
  var nowDate
  nowDate = newYear + '-' + (nowMonth < 10 ? '0' + nowMonth : nowMonth) + '-' + (day < 10 ? '0' + day : day)
  
  return nowDate
}

// 获取两日期间隔天数
export const getDiffDates = (start, end) => {
  var startTime = new Date(start).getTime()
  var endTime = new Date(end).getTime()
  var day = (endTime - startTime) / (24 * 60 * 60 * 1000)
  return day
}
//将时分秒转为时间戳
export const time_to_sec = (time) => {
　　if (time !== null) {
　　　　var s = "";
　　　　var hour = time.split(":")[0];
　　　　var min = time.split(":")[1];
　　　　var sec = time.split(":")[2];
　　　　s = Number(hour * 3600) + Number(min * 60) + Number(sec);
　　　　return s;
　　}
}

export const formatSeconds = (value) => {
  let result = parseInt(value)
  let h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600);
  let m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
  let s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));

  let res = '';
  if(h !== '00') res += `${h}时`;
  if(m !== '00') res += `${m}分`;
  if(s !== '00') res += `${s}秒`;
  return res;
}

//将时分转为时间戳
export function hhmm_to_sec(time) {
  if (time !== null) {
      var s = "";
      var hour = time.split(":")[0];
      var min = time.split(":")[1];
      s = Number(hour * 3600) + Number(min * 60);
      return s;
  }
}